
package com.market.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.market.bean.Product;
import com.market.dao.hibernate.GenericDaoHibernate;

/**
 * Data access object (DAO) for domain model class Product.
 * 
 * @see com.market.bean.Product
 * @author MyEclipse - Hibernate Tools
 */
public class ProductDAO extends GenericDaoHibernate<Product, Long> implements IProductDAO
{

   // private static final Logger log = LoggerFactory.getLogger(ProductDAO.class);

    public ProductDAO(Class<Product> persistentClass)
    {
        super(persistentClass);
        // TODO Auto-generated constructor stub
    }

    public ProductDAO()
    {
        super();
    }
    
    // property constants
    public static final String NAME = "name";

    public static final String STANDARD = "standard";

    public static final String PRICE = "price";

    public static final String VIPPRICE = "vipprice";

    public static final String DESCRIPTION = "description";

    public static final String HOT = "hot";

    public static final String TYPE = "type";

    public static final String SELECT_PRODUCT = "select new Product(p.id,p.category,p.name,p.standard, p.price,p.vipprice,p.dprice,p.description,p.adddate,p.hot,p.type,p.pic,p.unit) from Product p ";

    protected void initDao()
    {
        // do nothing
    }

    public Product findById(final long id)
    {
        return (Product)getHibernateTemplate().execute(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                Query query = s.createQuery(SELECT_PRODUCT + "where p.id=?");
                query.setLong(0, id);
                return query.uniqueResult();
            }

        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.market.dao.IProductDAO#getNewList(int)
     */
    public List getNewList(final String category, final int max)
    {
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                StringBuffer qStr = new StringBuffer();
                qStr.append(SELECT_PRODUCT);
                if (category != null)
                {
                    qStr.append(" where p.category='").append(category).append("'");
                }
                qStr.append(" order by p.adddate desc");
                Query query = s.createQuery(qStr.toString());
                query.setMaxResults(max);
                return query.list();
            }

        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.market.dao.IProductDAO#getHotList(int)
     */
    public List getHotList(final String category, final int max)
    {
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                StringBuffer qStr = new StringBuffer();
                qStr.append(SELECT_PRODUCT);
                if (category != null)
                {
                    qStr.append(" where p.category='").append(category).append("'");
                }
                qStr.append(" order by p.hot desc");
                Query query = s.createQuery(qStr.toString());
                query.setMaxResults(max);
                return query.list();
            }

        });
    }

    public List getDiscountList(final int start, final int max)
    {
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                Query query = s.createQuery(SELECT_PRODUCT + "where p.dprice>0");
                if (max != 0)
                {
                    query.setFirstResult(start).setMaxResults(max);
                }
                return query.list();
            }

        });
    }

    public int getCountByDiscount()
    {
        // TODO Auto-generated method stub
        Object o = getHibernateTemplate().execute(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                // TODO Auto-generated method stub
                Query query = s.createQuery("select count(*) from Product p where p.dprice>0");
                return query.uniqueResult();
            }

        });
        return ((Long)o).intValue();
    }

    public int getCountBySupplier(final long supplierid)
    {
        // TODO Auto-generated method stub
        Object o = getHibernateTemplate().execute(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                // TODO Auto-generated method stub
                Query query = s.createQuery("select count(*) from Product p where p.supplierid=?");
                query.setLong(0, supplierid);
                return query.uniqueResult();
            }

        });
        return ((Long)o).intValue();
    }

    private List findByProperty(final String property, final Object value, final int start, final int max)
    {
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {

            public Object doInHibernate(Session s) throws HibernateException, SQLException
            {
                Query query = s.createQuery(SELECT_PRODUCT + "where p." + property + "=:value");
                query.setParameter("value", value);
                if (max != 0)
                {
                    query.setFirstResult(start).setMaxResults(max);
                }
                return query.list();
            }

        });
    }

    public List findBySupplier(long supplierid, int start, int max)
    {
        // TODO Auto-generated method stub
        return findByProperty("supplierid", new Long(supplierid), start, max);
    }

    public Product findById(Class cla, long id)
    {
        DetachedCriteria detachCri = DetachedCriteria.forClass(cla);
        detachCri.add(Restrictions.eq("id", new Long(id)));
        List l = getHibernateTemplate().findByCriteria(detachCri);
        if (!l.isEmpty())
        {
            return (Product)l.get(0);
        }
        else
        {
            return null;
        }

    }

}
